package pile.pay.config.prometheus;


import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.Executor;

//配置一个监控自定义线程池的端点

@Configuration
public class ThreadPoolMonitorConfig {

    @Autowired
    private MeterRegistry meterRegistry;

    @Autowired
    @Resource(name = "asyncExecutor")
    private Executor asyncExecutor;


    // 线程池监控Bean  
    @PostConstruct
    public void registerThreadPoolMetrics() {

        ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) asyncExecutor;

        // 注册一个Gauge来监控线程池的活动线程数
        Gauge.builder("myAsyncThread", executor,ThreadPoolTaskExecutor::getActiveCount)
                .description("Number of active threads in the thread pool")
                .tags("thread_pool_name", "Feign异步线程-激活数") // 添加自定义标签
                .register(meterRegistry);


        // 注册一个Gauge来监控线程池的任务队列长度
        Gauge.builder("myAsyncThread", executor, ThreadPoolTaskExecutor::getMaxPoolSize)
                .description("Number of max in the thread pool")
                .tags("thread_pool_name", "Feign异步线程-最大数")
                .register(meterRegistry);

        Gauge.builder("myAsyncThread", executor, ThreadPoolTaskExecutor::getCorePoolSize)
                .description("Number of core in the thread pool")
                .tags("thread_pool_name", "Feign异步线程-核心数")
                .register(meterRegistry);
        // 你可以继续添加其他你感兴趣的度量，如已完成任务数、拒绝的任务数等
    }

}